function w = mypolyfit(X,Y,n,lambda)

%if (nargin < 3), n=2; end
%if (nargin < 4), lambda=0; end

m=size(X,1);

deg=repmat(0:(n-1),m,1);
Phi = repmat(X,1,n).^deg;

w=inv(lambda*eye(n)+Phi'*Phi) * Phi' * Y;

end